dummy-apartment 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bcd99e1fe4217d02817e2520ee29720279797317
4
- data.tar.gz: 81f13232754fdab7da1145e864cad78c2549330f
3
+ metadata.gz: 9c139f0d0104103f901fe458ba0dbf5035b5c24d
4
+ data.tar.gz: 38dde2e944cfa50ee493ecbf23c37bbc9b4701e2
5
5
  SHA512:
6
- metadata.gz: 56febfb9b8a3b00568596e751b4207b2f6db04f5bda24852499b47fbc9f76c08dfe81c6acc91b2bc8d5fac9df280f0fc4844a195ab63adf38adc9b53df24f832
7
- data.tar.gz: 36f309dc705fbcb0af98e964fdfdd5d83fc34e77aaa5d07e5bccc90def576096ec2292ab093e36efd636e95f282f819bd08351225fd575d6147e45dac3966e2c
6
+ metadata.gz: 5a415f9fd3f5faa2615de7a240dc1d2c706e9ae4e6dfb09fa46ec7929eaa9fecaa32eb855fce7208bcbb9451d14a37fe699507db28ae48f6eb7bba006f064457
7
+ data.tar.gz: 2c07a8d52b73e18487b2cbffa204e7514cba70e0fe892011685ba4e4752d7bdca9030a434a1bc5454ba37e4d80f9c9ce3cdc1e26c22e600ff503c71870888340
data/README.md CHANGED
@@ -24,7 +24,7 @@ Or just:
24
24
 
25
25
  ### Usage
26
26
 
27
- DummyApartment.generate
27
+ apartment = DummyApartment.generate
28
28
  #=> {:address => "群馬県長谷市60",
29
29
  # :building_name => "江頭Petit",
30
30
  # :geo => [36.878327083956236, 139.92838722714708],
@@ -32,6 +32,9 @@ Or just:
32
32
  # :room_floor => 2,
33
33
  # :room_number => "203"}
34
34
 
35
+ apartment.room_floor #=> 2
36
+ apartment[:room_floor] #=> 2
37
+ apartment['room_floor'] #=> 2
35
38
 
36
39
  ### Contributing
37
40
 
@@ -1,4 +1,4 @@
1
1
  class DummyApartment
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
4
4
 
@@ -11,29 +11,72 @@ class DummyApartment
11
11
 
12
12
  @@dic ||= Psych.load(File.open(YML).read)
13
13
 
14
+ ATTRIBUTES = %i(address building_name geo top_floor room_floor room_number)
15
+
16
+ attr_reader *ATTRIBUTES
17
+
14
18
  def self.generate
15
- address = gen_address
16
- name = gen_building_name
17
- geo = gen_long_lat
18
- top_floor = gen_top_floor
19
- room_floor = gen_room_floor(top_floor)
20
- room_number = gen_room_number(room_floor)
21
-
22
- {
23
- address: address, building_name: name, geo: geo,
24
- top_floor: top_floor, room_floor: room_floor,
25
- room_number: room_number
26
- }
19
+ address = gen_address
20
+ building_name = gen_building_name
21
+ geo = gen_long_lat
22
+ top_floor = gen_top_floor
23
+ room_floor = gen_room_floor(top_floor)
24
+ room_number = gen_room_number(room_floor)
25
+
26
+ values = ATTRIBUTES.map{ |attr| eval "#{attr}" }
27
+ DummyApartment.new(Hash[ATTRIBUTES.zip values])
28
+ end
29
+
30
+ def initialize(apartment_hash)
31
+ @hash = apartment_hash
32
+ assign_attributes
33
+ end
34
+
35
+ def [](key)
36
+ case key
37
+ when String; @hash[key.to_sym]
38
+ when Symbol; @hash[key]
39
+ else; raise
40
+ end
41
+ end
42
+
43
+ def []=(key, value)
44
+ case key
45
+ when String; assign(key.to_sym, value)
46
+ when Symbol; assign(key, value)
47
+ else; raise
48
+ end
49
+ end
50
+
51
+ def assign(key, value)
52
+ if ATTRIBUTES.member? key
53
+ @hash[key] = value
54
+ instance_variable_set("@#{key}".to_sym, value)
55
+ end
56
+ end
57
+
58
+ ATTRIBUTES.each do |attr|
59
+ eval <<-RUBY
60
+ def #{attr}=(value)
61
+ @hash[:#{attr}] = value
62
+ @#{attr} = value
63
+ end
64
+ RUBY
65
+ end
66
+
67
+ def method_missing(method_name, *args)
68
+ @hash.send(method_name, *args) if @hash.respond_to? method_name
27
69
  end
28
70
 
29
71
  def self.gen_address
30
72
  prefs = @@dic['prefectures']
31
73
  cities = @@dic['cities']
32
- number = NON_ZERO.sample
33
- number += rand(5).times.select{ NUM_DASH.sample }.join
34
- number.gsub!(/\-0|0\-/, '-')
35
74
 
36
- [prefs.sample, cities.sample, number].join
75
+ street = NON_ZERO.sample + NUM.sample(rand 1..3).join
76
+ dash_index = rand(street.length-1)
77
+ street.insert(dash_index, '-') if dash_index != 0 and dash_index != street.length
78
+
79
+ [prefs.sample, cities.sample, street].join
37
80
  end
38
81
 
39
82
  def self.gen_building_name
@@ -62,5 +105,13 @@ class DummyApartment
62
105
  end
63
106
 
64
107
  private_class_method *self.public_methods.grep(/\Agen_/)
108
+
109
+ private
110
+
111
+ def assign_attributes
112
+ ATTRIBUTES.each do |attr|
113
+ self.instance_variable_set("@#{attr}".to_sym, @hash[attr])
114
+ end
115
+ end
65
116
  end
66
117
 
@@ -1,10 +1,44 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe DummyApartment do
4
- let(:apartment) { DummyApartment.generate }
4
+ before do
5
+ @apartment = DummyApartment.generate
6
+ end
7
+
8
+ it 'should generate a DummyApartment object' do
9
+ expect(@apartment).to be_a DummyApartment
10
+ end
11
+
12
+ it 'should delegate to Hash' do
13
+ expect(@apartment.respond_to? :keys).to be_false
14
+ expect(@apartment.keys).to be_an Array
15
+ end
16
+
17
+ it 'should access the attribute via the method which has same name as the attribute' do
18
+ expect(@apartment.room_number).to eql @apartment[:room_number]
19
+ expect(@apartment.address).to eql @apartment[:address]
20
+ end
21
+
22
+ it 'should access the attribute using both String and Symbol' do
23
+ expect(@apartment['address']).to eql @apartment.address
24
+ expect(@apartment[:address]).to eql @apartment.address
25
+ end
26
+
27
+ it 'should be overwritten its attributes' do
28
+ @apartment.address = ''
29
+ expect(@apartment.address).to be_empty
30
+ @apartment[:address] = 'somewhere'
31
+ expect(@apartment.address).to eql 'somewhere'
32
+ end
33
+
34
+ it 'should have consistency' do
35
+ @apartment.address = 'somewhere'
36
+ expect(@apartment.address).to eql @apartment[:address]
37
+ expect(@apartment.address).to eql @apartment['address']
38
+ end
5
39
 
6
- it 'should generate a hash' do
7
- expect(apartment).to be_a Hash
40
+ it 'should raise when pass an argument which is not String or Symbol' do
41
+ expect { @apartment[0] }.to raise_error
8
42
  end
9
43
 
10
44
  it 'should have no public class method starts with "gen_"' do
@@ -13,7 +47,7 @@ describe DummyApartment do
13
47
  end
14
48
 
15
49
  describe 'Building Name' do
16
- let(:name){ apartment[:building_name] }
50
+ let(:name){ @apartment[:building_name] }
17
51
 
18
52
  it 'should not be empty' do
19
53
  expect(name).not_to be_empty
@@ -21,7 +55,7 @@ describe DummyApartment do
21
55
  end
22
56
 
23
57
  describe 'Address' do
24
- let(:address){ apartment[:address] }
58
+ let(:address){ @apartment[:address] }
25
59
 
26
60
  it 'should match address format' do
27
61
  expect(address).to match /[都道府県].+[市町村].*[0-9]/
@@ -29,7 +63,7 @@ describe DummyApartment do
29
63
  end
30
64
 
31
65
  describe 'Geo' do
32
- let(:geo){ apartment[:geo] }
66
+ let(:geo){ @apartment[:geo] }
33
67
 
34
68
  it 'should be a couple of Float' do
35
69
  expect(geo.map(&:class)).to eql [Float, Float]
@@ -37,8 +71,8 @@ describe DummyApartment do
37
71
  end
38
72
 
39
73
  describe 'Room Floor and Top Floor' do
40
- let(:top_floor) { apartment[:top_floor] }
41
- let(:room_floor){ apartment[:room_floor] }
74
+ let(:top_floor) { @apartment[:top_floor] }
75
+ let(:room_floor){ @apartment[:room_floor] }
42
76
 
43
77
  it 'should have valid floor information' do
44
78
  expect( top_floor).to be >= room_floor
@@ -48,8 +82,8 @@ describe DummyApartment do
48
82
  end
49
83
 
50
84
  describe 'Room Number' do
51
- let(:room_floor) { apartment[:room_floor] }
52
- let(:room_number){ apartment[:room_number] }
85
+ let(:room_floor) { @apartment[:room_floor] }
86
+ let(:room_number){ @apartment[:room_number] }
53
87
 
54
88
  it 'should be a three-character string' do
55
89
  expect(room_number).to be_a String
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dummy-apartment
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joe-noh
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-05-15 00:00:00.000000000 Z
11
+ date: 2014-05-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler